//
//  StatusCell.swift
//  SinaWeibo
//
//  Created by 周小伟 on 16/8/24.
//  Copyright © 2016年 周小伟. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellMargin:CGFloat = 12
let StatusCellIconWidth:CGFloat = 35

class StatusCell: UITableViewCell {
    
    var bottomConstrains:Constraint?
    
    var status:Status?{
        didSet{
            originalView.status = status
            
            self.bottomConstrains?.uninstall()
            
            if let re = status?.retweeted_status{
                retweetedView.retweetedStatus = re
                retweetedView.hidden = false
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomConstrains = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
            }else{
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomConstrains = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(bottomView)
        
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
        }
        
        retweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        
        bottomView.snp_makeConstraints { (make) in
            make.left.equalTo(contentView.snp_left)
            self.bottomConstrains = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
        }
        
        contentView.snp_makeConstraints { (make) in
            make.bottom.equalTo(bottomView.snp_bottom)
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
    }
    
    
    
    private lazy var originalView:StatusOriginalView = StatusOriginalView()
    private lazy var bottomView:StatusCellBottomView = StatusCellBottomView()
    private lazy var retweetedView:ReweetedStatusView = ReweetedStatusView()

}
